<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="100px"
    >
      <el-form-item label="课程名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入课程名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="商品名称" prop="commodityName">
        <el-input
          v-model="queryParams.commodityName"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="老师姓名" prop="teacherName">
        <el-input
          v-model="queryParams.teacherName"
          placeholder="请输入老师姓名"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="商品价格" prop="commodityPrice">
        <el-input
          v-model="queryParams.commodityPrice"
          placeholder="请输入课程商品价格"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="授课总时长" prop="teachingAllTime">
        <el-input
          v-model="queryParams.teachingAllTime"
          placeholder="请输入授课总时长"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="单次授课时长" prop="teachingTime">
        <el-input
          v-model="queryParams.teachingTime"
          placeholder="请输入单次授课时长"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="开课时间" prop="beginTime">
        <el-date-picker
          v-model="queryParams.beginTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="结课时间" prop="endTime">
        <el-date-picker
          v-model="queryParams.endTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="vip标识" prop="vipFlag">
        <el-select
          v-model="queryParams.vipFlag"
          placeholder="请选择vip标识"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.TRAIN_COMMODITY_VIP_FLAG)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="报名人数" prop="applicantNumber">
        <el-input
          v-model="queryParams.applicantNumber"
          placeholder="请输入报名人数"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="课程标记" prop="courseFlag">
        <el-select
          v-model="queryParams.courseFlag"
          placeholder="请选择课程标记"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.TRAIN_COMMODITY_FLAG)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="上架状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择上架状态"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.TRAIN_COURSE_COMMODITY_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon icon="ep:search" class="mr-5px" />
          搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon icon="ep:refresh" class="mr-5px" />
          重置
        </el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['train:course-commodity:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" />
          新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['train:course-commodity:export']"
        >
          <Icon icon="ep:download" class="mr-5px" />
          导出
        </el-button>
        <el-button
          type="danger"
          plain
          :disabled="isEmpty(checkedIds)"
          @click="handleDeleteBatch"
          v-hasPermi="['train:course-commodity:delete']"
        >
          <Icon icon="ep:delete" class="mr-5px" />
          批量删除
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table
      row-key="id"
      v-loading="loading"
      :data="list"
      :stripe="true"
      :show-overflow-tooltip="true"
      @selection-change="handleRowCheckboxChange"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column label="商品名称" align="center" prop="commodityName" width="100" />
      <el-table-column label="课程名称" align="center" prop="name" width="100" />
      <el-table-column label="老师姓名" align="center" prop="teacherName" width="100" />
      <el-table-column label="封面" align="center" prop="courseCoverUrl" width="100">
        <template #default="scope">
          <el-tooltip
            v-if="scope.row.courseCoverUrl"
            :content="scope.row.courseCoverUrl"
            placement="top"
          >
            <ImagePreview v-if="scope.row.courseCoverUrl" :src="scope.row.courseCoverUrl" />
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="商品价格" align="center" prop="commodityPrice" width="100" />
      <el-table-column label="授课总时长" align="center" prop="teachingAllTime" width="110" />
      <el-table-column label="单次授课时长" align="center" prop="teachingTime" width="110" />
      <el-table-column label="开课时间" align="center" prop="beginTime" width="100" :formatter="dateFormatter2" />
      <el-table-column label="结课时间" align="center" prop="endTime" width="100" :formatter="dateFormatter2"  />
      <el-table-column label="vip标识" align="center" prop="vipFlag" width="100">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.TRAIN_COMMODITY_VIP_FLAG" :value="scope.row.vipFlag" />
        </template>
      </el-table-column>
      <el-table-column label="上架状态" align="center" prop="status" width="100">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.TRAIN_COURSE_COMMODITY_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="报名人数" align="center" prop="applicantNumber" width="100" />
      <el-table-column label="课程标记" align="center" prop="courseFlag" width="100">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.TRAIN_COMMODITY_FLAG" :value="scope.row.courseFlag" />
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="操作" align="center" min-width="120px" fixed="right">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['train:course-commodity:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['train:course-commodity:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <CourseCommodityForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { getStrDictOptions, DICT_TYPE } from '@/utils/dict'
import { isEmpty } from '@/utils/is'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { CourseCommodityApi, CourseCommodity } from '@/api/train/coursecommodity'
import CourseCommodityForm from './CourseCommodityForm.vue'
import { dateFormatter2 } from '@/utils/formatTime'

/** 课程商品管理 列表 */
defineOptions({ name: 'CourseCommodity' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<CourseCommodity[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  courseId: undefined,
  teacherId: undefined,
  courseCoverUrl: undefined,
  commodityPrice: undefined,
  teachingAllTime: [],
  teachingTime: [],
  beginTime: [],
  endTime: [],
  vipFlag: undefined,
  applicantNumber: undefined,
  courseFlag: undefined,
  createTime: [],
  name: undefined,
  teacherName: undefined,
  commodityName: undefined,
  status: undefined
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await CourseCommodityApi.getCourseCommodityPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await CourseCommodityApi.deleteCourseCommodity(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 批量删除课程商品管理 */
const handleDeleteBatch = async () => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    await CourseCommodityApi.deleteCourseCommodityList(checkedIds.value)
    message.success(t('common.delSuccess'))
    await getList()
  } catch {}
}

const checkedIds = ref<number[]>([])
const handleRowCheckboxChange = (records: CourseCommodity[]) => {
  checkedIds.value = records.map((item) => item.id)
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await CourseCommodityApi.exportCourseCommodity(queryParams)
    download.excel(data, '课程商品管理.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
